<template>
  <div class="flex-center h-screen bg-gray-100">
    <a-card class="w-96">
      <template #title>
        <div class="text-center text-xl font-bold">注册</div>
      </template>
      <a-form
        :model="form"
        @finish="handleSubmit"
      >
        <a-form-item
          name="username"
          :rules="[{ required: true, message: '请输入用户名' }]"
        >
          <a-input
            v-model:value="form.username"
            placeholder="请输入用户名"
          />
        </a-form-item>
        <a-form-item
          name="password"
          :rules="[{ required: true, message: '请输入密码' }]"
        >
          <a-input-password
            v-model:value="form.password"
            placeholder="请输入密码"
          />
        </a-form-item>
        <a-form-item
          name="confirmPassword"
          :rules="[{ required: true, message: '请确认密码' }]"
        >
          <a-input-password
            v-model:value="form.confirmPassword"
            placeholder="请再次输入密码"
          />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" html-type="submit" block>
            注册
          </a-button>
        </a-form-item>
        <div class="text-center">
          <router-link to="/auth/login" class="text-primary">
            已有账号？去登录
          </router-link>
        </div>
      </a-form>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'

const router = useRouter()

const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
})

const handleSubmit = async () => {
  if (form.password !== form.confirmPassword) {
    message.error('两次输入的密码不一致')
    return
  }
  try {
    // TODO: 调用注册 API
    message.success('注册成功')
    router.push('/auth/login')
  } catch (error) {
    message.error('注册失败')
  }
}
</script>